package com.ioPackage;

import java.io.File;

/**
 * @ClassName RecursionDemo2
 * @Description 文件搜索
 * @Author 2214361854
 * @Date 2022/2/27 17:35
 * @Version 1.0
 */
public class RecursionDemo2 {
    public static void main(String[] args) {
        //D盘文件：FileDemo.java
        searchFile(new File("D:/"),"FileDemo.java");
    }
    public static void searchFile(File dir, String fileName){
        if (dir != null && dir.isDirectory()) {
            File[] files = dir.listFiles();
            if (files != null && files.length > 0) {
                  for (File file : files) {
                      if (file.isFile()) {
                          if (file.getName().contains(fileName)) {
                              System.out.println("找到了："+file.getAbsolutePath());
                          }
                      }else {
                          searchFile(file,fileName);
                      }
                 }
            }
        } else {
            System.out.println("当前搜索的不是文件夹："+dir);
        }
    }
}
